<?php
namespace App\Http\Middleware;
use Closure;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class GetUserFromToken
{
    public function handle($request, Closure $next)
    {
        return $next($request);
//        $auth = JWTAuth::parseToken();
        if (! $token = $auth->setRequest($request)->getToken()) {
            return response()->json([
                'status_code' => 291,
                'message' => 'token_not_provided',
                'data' => '未提供有效token',
            ]);
        }

        try {
            $user = $auth->authenticate($token);
        } catch (TokenExpiredException $e) {
            return response()->json([
                'status_code' => 292,
                'message' => 'token_expired',
                'data' => 'token已过期，请重新登录',
            ]);
        } catch (JWTException $e) {
            return response()->json([
                'status_code' => 293,
                'message' => 'token_invalid',
                'data' => '无效token',
            ]);
        }
        if (! $user) {
            return response()->json([
                'status_code' => 294,
                'message' => 'user_not_found',
                'data' => '未找到登录用户',
            ]);
        }

        return response()->json([
            'status_code' => 294111,
            'message' => 'user_not_found',
            'data' => '未找到登录用户',
        ]);
        //$this->events->fire('tymon.jwt.valid', $user);
        return $next($request);
    }
}